//
//  HJDWeightViewController.m
//  JYHospital
//
//  Created by 胡金东 on 16/5/13.
//  Copyright © 2016年 胡金东. All rights reserved.
//

#import "HJDWeightViewController.h"
//#import "WareViewController.h"
#import "HJDWeightGuideViewController.h"

@interface HJDWeightViewController ()

@property (nonatomic,strong) UIButton * lblshow;
//身高
@property (nonatomic,strong) UITextField  *heightField;
//孕前体重
@property (nonatomic,strong) UITextField  *masstextField;
//今日体重
@property (nonatomic,strong) UITextField  *nowmasstextField;




@end

@implementation HJDWeightViewController


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
//    self.navigationItem.rightBarButtonItem = NO;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setUpText];
    
    [self setUpBtn];
    self.title = @"称体重";
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont systemFontOfSize:19],
       NSForegroundColorAttributeName:[UIColor getColorWithHexNumber:@"848484"]}];

//    分享按钮
//    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(shareBtnClick) image:@"矢量智能对象" HighImage:nil];
    
//    UIButton *shareBtn = [[UIButton alloc]init];
//    [shareBtn setBackgroundImage:[UIImage imageNamed:@"矢量智能对象"] forState:UIControlStateNormal];
//    self.navigationItem.rightBarButtonItem =
    //添加手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
    tap.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tap];
    
}

//隐藏键盘
-(void)viewTapped:(UITapGestureRecognizer*)tap
{
    
    [self.view endEditing:YES];
    
}


-(void)setUpText
{
    CGFloat lblX = 0 ;
    CGFloat lblY = HJDNavigHeight;
    CGFloat lblW = HJDWidth;
    CGFloat lblH = 40;
    UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(lblX, lblY, lblW, lblH)];
    lbl.backgroundColor = [UIColor getColorWithHexNumber:@"CFCFCF"];
    lbl.text = @"  点击数一数今日胎动数，为您预测宝宝健康值";
    lbl.font = [UIFont systemFontOfSize:14];
    lbl.textColor = [UIColor lightGrayColor];
    [self.view addSubview:lbl];
    
    
    CGFloat heightX = HJDHorizontalMargin ;
    CGFloat heightY = CGRectGetMaxY(lbl.frame)+5;
    CGFloat heightW = HJDWidth - HJDHorizontalMargin-HJDHorizontalMargin*2;
    CGFloat heightH = 40;
    self.heightField = [[UITextField alloc]initWithFrame:CGRectMake(heightX, heightY, heightW, heightH)];
    self.heightField.clearButtonMode = UITextFieldViewModeAlways;
    self.heightField.keyboardType = UIKeyboardTypeDecimalPad;

    self.heightField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100,heightH )];
    self.heightField.leftViewMode = UITextFieldViewModeAlways;
    UILabel *heightLbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100,heightH )];
    heightLbl.text = @"输入身高";
    heightLbl.textColor = [UIColor getColorWithHexNumber:@"323232"];
    [self.heightField.leftView addSubview:heightLbl];
    [self.view addSubview:self.heightField];
    
    CGFloat hLblX = HJDWidth - HJDHorizontalMargin-12;
    CGFloat hLblY = heightY;
    CGFloat hLblW = 40;
    CGFloat hLblH = heightH;
    UILabel *hLbl = [[UILabel alloc]initWithFrame:CGRectMake(hLblX, hLblY, hLblW, hLblH)];
    hLbl.text = @"m";
    [self.view addSubview:hLbl];

    //    横线
    CGFloat viewX = HJDHorizontalMargin ;
    CGFloat viewY = CGRectGetMaxY(self.heightField.frame);
    CGFloat viewW = HJDWidth - HJDHorizontalMargin ;
    CGFloat viewH = 1;
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(viewX, viewY, viewW, viewH)];
    view.backgroundColor = [UIColor getColorWithHexNumber:@"CFCFCF"];
    [self.view addSubview:view];
    
    CGFloat massX = HJDHorizontalMargin ;
    CGFloat massY = CGRectGetMaxY(view.frame)+5;
    CGFloat massW = HJDWidth - HJDHorizontalMargin-HJDHorizontalMargin*2;
    CGFloat massH = 40;
    self.masstextField = [[UITextField alloc]initWithFrame:CGRectMake(massX, massY,massW, massH)];
   
    self.masstextField.keyboardType = UIKeyboardTypeDecimalPad;
    self.masstextField.clearButtonMode = UITextFieldViewModeAlways;
    
    self.masstextField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100,massH )];
    self.masstextField.leftViewMode = UITextFieldViewModeAlways;
    UILabel *masstextLbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100,massH )];
    masstextLbl.text = @"孕前体重";
    masstextLbl.textColor = [UIColor getColorWithHexNumber:@"323232"];
    [self.masstextField.leftView addSubview:masstextLbl];
    
    [self.view addSubview:self.masstextField];
    
    CGFloat mLblX = HJDWidth - HJDHorizontalMargin *2 +5;
    CGFloat mLblY = massY;
    CGFloat mLblW = 40;
    CGFloat mLblH = massH;
    UILabel *mLbl = [[UILabel alloc]initWithFrame:CGRectMake(mLblX, mLblY, mLblW, mLblH)];
    mLbl.text = @"kg";
    [self.view addSubview:mLbl];
    //    横线
    CGFloat nowmassviewX = HJDHorizontalMargin ;
    CGFloat nowmassviewY = CGRectGetMaxY(self.masstextField.frame);
    CGFloat nowmassviewW = HJDWidth - HJDHorizontalMargin ;
    CGFloat nowmassviewH = 1;
    UIView *nowmassview = [[UIView alloc]initWithFrame:CGRectMake(nowmassviewX, nowmassviewY, nowmassviewW, nowmassviewH)];
    nowmassview.backgroundColor = [UIColor getColorWithHexNumber:@"CFCFCF"];
    [self.view addSubview:nowmassview];
    CGFloat X = HJDHorizontalMargin ;
    CGFloat Y = CGRectGetMaxY(nowmassview.frame) + 5;
    CGFloat W = HJDWidth - HJDHorizontalMargin -HJDHorizontalMargin*2;
    CGFloat H = 40;
    self.nowmasstextField = [[UITextField alloc]initWithFrame:CGRectMake(X, Y, W, H)];
    self.nowmasstextField.keyboardType = UIKeyboardTypeDecimalPad;
    self.nowmasstextField.clearButtonMode = UITextFieldViewModeAlways;
    self.nowmasstextField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100,H )];
    self.nowmasstextField.leftViewMode = UITextFieldViewModeAlways;
    UILabel *nowmassLbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100,H )];
    nowmassLbl.text = @"今日体重";
    nowmassLbl.textColor = [UIColor getColorWithHexNumber:@"323232"];
    [self.nowmasstextField.leftView addSubview:nowmassLbl];

    [self.view addSubview:self.nowmasstextField];
    
    
    CGFloat nLblX = HJDWidth - HJDHorizontalMargin *2 + 5;
    CGFloat nLblY = Y;
    CGFloat nLblW = 40;
    CGFloat nLblH = H;
    UILabel *nLbl = [[UILabel alloc]initWithFrame:CGRectMake(nLblX, nLblY, nLblW, nLblH)];
    nLbl.text = @"kg";
    [self.view addSubview:nLbl];

     //    横线
    CGFloat massviewX = HJDHorizontalMargin ;
    CGFloat massviewY = CGRectGetMaxY(self.nowmasstextField.frame);
    CGFloat massviewW = HJDWidth - HJDHorizontalMargin ;
    CGFloat massviewH = 1;
    UIView *massview = [[UIView alloc]initWithFrame:CGRectMake(massviewX, massviewY, massviewW, massviewH)];
    massview.backgroundColor = [UIColor getColorWithHexNumber:@"CFCFCF"];
    [self.view addSubview:massview];
    
    CGFloat imageVX = HJDWidth  -90;
    CGFloat imageVY = CGRectGetMaxY(self.nowmasstextField.frame)+HJDVerticalMargin+2;
    CGFloat imageVW = 13;
    CGFloat imageVH = 29/2;
    UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(imageVX, imageVY, imageVW, imageVH)];
    imageV.image = [UIImage imageNamed:@"孕期指南脚丫"];
    [self.view addSubview:imageV];
    
    CGFloat lblshowX = HJDWidth  -80;
    CGFloat lblshowY = CGRectGetMaxY(self.nowmasstextField.frame)+HJDVerticalMargin;
    CGFloat lblshowW = 70;
    CGFloat lblshowH = 20;
    self.lblshow = [[UIButton alloc]initWithFrame:CGRectMake(lblshowX, lblshowY, lblshowW, lblshowH)];
    [self.lblshow setTitle:@"体重指南" forState:UIControlStateNormal];
    [self.lblshow setTitleColor:[UIColor getColorWithHexNumber:@"FF92A2"] forState:UIControlStateNormal];
    self.lblshow.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.lblshow addTarget:self action:@selector(weightBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.lblshow];
    
    
}

-(void)setUpBtn
{
    CGFloat submitbtnX = 40;
    CGFloat submitbtnY = CGRectGetMaxY(self.lblshow.frame) + HJDVerticalMargin * 2;
    CGFloat submitbtnW = HJDWidth - 40 * 2;
    CGFloat submitbtnH = 60;
    
    UIButton *submitbtn = [[UIButton alloc]initWithFrame:CGRectMake(submitbtnX, submitbtnY,submitbtnW, submitbtnH)];
    [submitbtn setTitle:@"提交" forState:UIControlStateNormal];
    //    要失效
    //    seebtn.font = [UIFont systemFontOfSize:19];
    submitbtn.titleLabel.font = [UIFont systemFontOfSize:19];
    [submitbtn setRadius:10 borderWith:0 borderColor:nil];
    [submitbtn setBackgroundColor:[UIColor getColorWithHexNumber:@"5CC6AC"]];
    [submitbtn addTarget:self action:@selector(submitbtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:submitbtn];
    
}

//-(void)shareBtnClick
//{
//    NSLog(@"分享");
//}

-(void)weightBtnClick{
    HJDWeightGuideViewController *guide = [[HJDWeightGuideViewController alloc]init];
    [self.navigationController pushViewController:guide animated:YES];
}

-(void)submitbtnClick
{
    float height = [self.heightField.text floatValue];
    float mass = [self.masstextField.text floatValue];
    float now = [self.nowmasstextField.text floatValue];
    
    float normWeight = height*height*21;
    float idealWeight = (now/(height*height)*0.88+6.65)*height*height;
    float BMI = now/(height*height);
    float fat = mass/normWeight *100;
    
//    NSLog(@"%@----%@----%@----%@",self.heightField.text,self.masstextField.text,self.nowmasstextField.text,str);
    //初始化提示框；
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:[NSString stringWithFormat:@"孕前标准体重=%.2fkg\n孕前肥胖度=%.2f%%\nBMI值=%.2f\n安产理想体重=%.2f",normWeight,fat,BMI,idealWeight] preferredStyle:  UIAlertControllerStyleActionSheet];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //点击按钮的响应事件；
    }]];
    
    //弹出提示框；
    [self presentViewController:alert animated:YES completion:nil];
    
    
    
//    WareViewController * ware = [[WareViewController alloc]init];
//    [self.navigationController pushViewController:ware animated:YES];
}

@end
